package com.example.chat.dao;

import com.dragonsoftbravo.jpadsl.JPADslBaseDao;
import com.dragonsoftbravo.jpadsl.PredicateBuilder;
import com.example.chat.model.*;
import com.example.chat.model.dto.PermissionDTO;
import com.querydsl.core.types.Projections;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * @Description
 * @Date 2022/11/17 0:16
 * @Author pq
 */
@Repository
public class RolePermissionDao extends JPADslBaseDao<RolePermission,String> {

    public QRolePermission basePath(){
        return QRolePermission.rolePermission;
    }

    public RolePermissionDao(EntityManager entityManager){
        setEntityManager(entityManager);
    }

    public List<PermissionDTO> getRolePermission(String empId){
        QEmployeeRole employeeRole=QEmployeeRole.employeeRole;
        PredicateBuilder pb=PredicateBuilder.buildQ();
        pb.eq(employeeRole.employeeId,empId);
        return queryFactory().select(Projections.bean(PermissionDTO.class,
                basePath().role.name.as("roleName"),
                basePath().permission.code.as("permissionCode")
                ))
                .from(basePath())
                .leftJoin(employeeRole).on(employeeRole.roleId.eq(basePath().roleId))
                .where(pb.get())
                .fetch();
    }
}
